﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data;
using System.IO;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Diagnostics;
using System.ComponentModel;
namespace kiosk
{

    public enum DataOption
    {
        TextDataType = 0,
        TableDataType,
        None = -1

    }


  
    public enum MySQLMessage
    { 
         HostError = -1,
         UnknownDataBase = -2,
         AuthencationError = -3,
         Success = 1,
         UnKnown = -100
    
    }


    public enum RotateView
    {

        Default = 1,
        RotateBy90 = 2,
        RotateBy180 = 4,
        RotateBy270 = 8,
        RotateByFlipx = 16,
        RotateByFlipy = 32,
        RotateFlipxy = 64,


    }

   

    public enum FitView
    {

        FitToWidth = 255,
        FitToHeight = 128

    }


    public enum language
    {

        english = 1,
        bengali,
        nepali,
        none = -1
    };

    // The Item Class
    public class Item
    {
        protected  Int32 itemid;
        protected string itemtext;
        public Item()
        {

        }
        public Item(Int32 id, string text)
        {
            this.itemid = id;
            this.itemtext = text;

        }
        public Int32 ItemID
        {
            set
            {
                itemid = value;
            }
            get
            {
                return itemid;
            }


        }

        public string ItemText
        {
            set
            {

                itemtext = value;
            }
            get
            {
                return itemtext;

            }


        }

    }


    public class ExtItem : Item
    {


        private string FontTxt;
        public ExtItem()
        { 
        
        
        }


         public ExtItem(Int32 id, string text,string fonttxt ):base (id,text)
        {
             
            
             this.FontTxt = fonttxt;

        }
      new public Int32 ItemID
        {
            set
            {
                this.itemid = value;
            }
            get
            {
                return itemid;
            }


        }

   new   public string ItemText
        {
            set
            {

                this.itemtext = value;
            }
            get
            {
                return itemtext;

            }


        }

        public string Font
        {
            set
            {
                this.FontTxt = value;

            
            }
            get
            {
                return FontTxt;
            
            }
        
        }





    
    
    
    
    }


    public class DisplayOption
    {

        //
        private String font;

        //
        
        private int dataOpt;
        private string DisplayText;
        public DisplayOption(Int32 dOpt, string dText)
        {
            this.dataOpt = dOpt;
            this.DisplayText = dText;
            font = String.Empty;

        }
        public DisplayOption() {

            Font font1 = new Font("Arial", 14, FontStyle.Italic);

            TypeConverter converter = TypeDescriptor.GetConverter(typeof(Font));
            // Saving Font object as a string
            this.font = converter.ConvertToString(font1);

        
        
        }
        public override string ToString()
        {
            return this.DisplayText;
        }

        public string dText
        {
            set
            {
                this.DisplayText = value;
            }
            get
            {
                return this.DisplayText;

            }

        }

        public Int32 dataOption
        {

            get
            {
                return this.dataOpt;

            }
            set
            {
                this.dataOpt = value;

            }

        }

        public string DefaultFont
        {
            get
            {
                return this.font;
            
            }
            
        
        
        }



    }

    
    public class WinApi
    {

        [DllImport("user32.dll", EntryPoint = "GetSystemMetrics")]
        public static extern int GetSystemMetrics(int which);

        [DllImport("user32.dll")]

        public static extern void
            SetWindowPos(IntPtr hwnd, IntPtr hwndInsertAfter,
                         int X, int Y, int width, int height, uint flags);

        private const int SM_CXSCREEN = 0;
        private const int SM_CYSCREEN = 1;
        private static IntPtr HWND_TOP = IntPtr.Zero;
        private const int SWP_SHOWWINDOW = 64; // 0×0040

        public static int ScreenX
        {
            get { return GetSystemMetrics(SM_CXSCREEN); }
        }

        public static int ScreenY
        {
            get { return GetSystemMetrics(SM_CYSCREEN); }
        }

        public static void SetWinFullScreen(IntPtr hwnd)
        {
            SetWindowPos(hwnd, HWND_TOP, 0, 0, ScreenX, ScreenY, SWP_SHOWWINDOW);
        }
    }






    
    public class GeneralOpt
    {

        public static DataTable DTable;

        public static string ServerAddress;
        public static string DatabaseName;
        public static string DatabaseUserName;
        public static string DatabaseUserPass;
        public static int ServerPort;

        public static bool pdfHeadingChanged = false;
        public static bool pdfDataChanged = false;
        public static bool pictChanged = false;
        public static int pictIDChange = -1;
        public static int ThumBillWidth = 290;
        public static int ThumbillHeight = 191;
        public static string BackGroundImageFolder = "BGImages";
        public static String AppThumbillFolderName = "PctureThumbils";
        public static String AppSettingsFileName = "AppSettings.xml";
        public static String AppPictFolder = "Pictures";
        public static String AppPictCacheFolder = "PictCache";
        public GeneralOpt()
        { 
        
        }

                
        /// <summary>
        /// gets all the language buttons from the database
        /// </summary>
        public static int getLanguageButtons()
        {
            DataBaseHandler handler = new DataBaseHandler();
            GeneralOpt.DTable = handler.getAllLanData();

            if (GeneralOpt.DTable.Rows.Count < 1)
                return -1;
            else
                return 1;
            

         }



        /// <summary>
        /// Checks the background images settings in the xml file
        /// </summary>
        /// <returns></returns>
        public static int checkBgSettings()
        {

            //  read the xml file 
            XDocument docu = XDocument.Load(Path.Combine(Application.StartupPath, GeneralOpt.AppSettingsFileName));
            // load the specific image from the background
            XElement element = docu.Root.Element("BgImages");
            if (element == null)
                return -1;
            else
                return 1;
        
        
        }


// The function that loads the application settings and checks them too
        // the function also parpares the database settings
        public static int loadAppSetting()
        {
            try
            {


                String settingFpath = System.IO.Path.Combine(Application.StartupPath, GeneralOpt.AppSettingsFileName);
                if (File.Exists(settingFpath))
                {

                   
                    XDocument docu = XDocument.Load(settingFpath);
                    if (docu.Root != null)
                    {
                        XElement server = docu.Root.Element("Server");
                        if (server != null)
                        {

                            server = server.Element("SvrAddress");
                            if (server != null)
                            {
                                GeneralOpt.ServerAddress = server.Value;
                                GeneralOpt.ServerPort = Convert.ToInt32(server.Attribute("SvrPort").Value);

                            }
                            else
                                return -1;
                        }
                        else
                            return -1;

                        XElement Database = docu.Root.Element("Database");

                        if (Database != null)
                        {

                            Database = Database.Element("Name");



                            if (Database != null)
                            {
                                GeneralOpt.DatabaseName = Database.Value;
                                GeneralOpt.DatabaseUserName = Database.Attribute("UserName").Value;
                                GeneralOpt.DatabaseUserPass = Database.Attribute("UserPassword").Value;

                            }
                            else
                                return -1;
                        }

                        else
                            return -1;



                    }
                    else
                        return -1;

                }

                return 1;

            }
            catch (Exception ex)
            {
              
                Console.WriteLine(ex.Message);
                return -1;
            
            }
          
        
        }

        /// The image resizing function
        /// 

        public static Image ImageResize(Image img ,int newWidth, int newHeight)
        {
            if (newWidth != 0 && newHeight != 0)
            {

                Bitmap temp = (Bitmap)img;


                Bitmap bmap = new Bitmap(newWidth, newHeight, temp.PixelFormat);

                double nWidthFactor = (double)temp.Width / (double)newWidth;
                double nHeightFactor = (double)temp.Height / (double)newHeight;

                double fx, fy, nx, ny;
                int cx, cy, fr_x, fr_y;
                Color color1 = new Color();
                Color color2 = new Color();
                Color color3 = new Color();
                Color color4 = new Color();
                byte nRed, nGreen, nBlue;

                byte bp1, bp2;

                for (int x = 0; x < bmap.Width; ++x)
                {
                    for (int y = 0; y < bmap.Height; ++y)
                    {

                        fr_x = (int)Math.Floor(x * nWidthFactor);
                        fr_y = (int)Math.Floor(y * nHeightFactor);
                        cx = fr_x + 1;
                        if (cx >= temp.Width) cx = fr_x;
                        cy = fr_y + 1;
                        if (cy >= temp.Height) cy = fr_y;
                        fx = x * nWidthFactor - fr_x;
                        fy = y * nHeightFactor - fr_y;
                        nx = 1.0 - fx;
                        ny = 1.0 - fy;

                        color1 = temp.GetPixel(fr_x, fr_y);
                        color2 = temp.GetPixel(cx, fr_y);
                        color3 = temp.GetPixel(fr_x, cy);
                        color4 = temp.GetPixel(cx, cy);

                        // Blue
                        bp1 = (byte)(nx * color1.B + fx * color2.B);

                        bp2 = (byte)(nx * color3.B + fx * color4.B);

                        nBlue = (byte)(ny * (double)(bp1) + fy * (double)(bp2));

                        // Green
                        bp1 = (byte)(nx * color1.G + fx * color2.G);

                        bp2 = (byte)(nx * color3.G + fx * color4.G);

                        nGreen = (byte)(ny * (double)(bp1) + fy * (double)(bp2));

                        // Red
                        bp1 = (byte)(nx * color1.R + fx * color2.R);

                        bp2 = (byte)(nx * color3.R + fx * color4.R);

                        nRed = (byte)(ny * (double)(bp1) + fy * (double)(bp2));

                        bmap.SetPixel(x, y, System.Drawing.Color.FromArgb(255, nRed, nGreen, nBlue));
                    }
                }


                return (Image)bmap;
            }
            else
            {
                return null;
            
            }

        }










 }



    

/// <summary>
///  the following class stroes the application cache settings
/// </summary>

    class ApplicationCache
    {



        public static MainForm AppMainFormWindow;
        public static DisplayButtons AppFormDisplayButton;
        public static PdfViewer AppPdfViewer;
        public static DataBaseHandler AppDataHandler;

        public static List<Item> TickerMessages;
        public static Image MainFormBGImage;
        public static Image ButtonWindowImage;
        public static Image PdfWindowImage;


        public static Int32 mTopMargin = 3;
        public static Int32 mButtomMargin = 15;
        public static Int32 mLeftMargin = 5;
        public static Int32 mRightMargin = 10;
        public static Int32 mWidth = 500 ;
        public static Int32 mHeight = 110;

        public static Int32 bTopMargin = 6;
        public static Int32 bButtomMargin = 6;
        public static Int32 bLeftMargin = 5;
        public static Int32 bRightMargin = 5;
        public static Int32 bWidth = 500;
        public static Int32 bHeight = 85;
        public static Int32 bNoButton = 4; 

        public static Int32 gTopMargin = 5;
        public static Int32 gButtomMargin = 5;
        public static Int32 gLeftMargin = 5;
        public static Int32 gRightMargin = 5;
        public static Int32 gWidth = 100;
        public static Int32 gHeight = 50;


        /// <summary>
        /// The function that creates the button buffer 
        /// </summary>
        /// <param name="handler"></param>
        public static void CreateButtonBuffer(DataBaseHandler handler)
        {

            // Now loop through all the group and insert in the buffer where the group has more then one item

            //List<int> firstGroup = handler.getPrimaryBtnGroup();



        }



        // the function that loads the bg images in the cache


        // Load the background images 
        /// <summary>
        /// this function loads the background images of the application 
        /// </summary>
        /// <returns></returns>



        public static void LoadBgImages()
        {


            /// Clear up the memory
            /// 
            if (ApplicationCache.MainFormBGImage != null)
            {
                ApplicationCache.MainFormBGImage.Dispose();
                ApplicationCache.MainFormBGImage = null;
            
            }
            
            if(ApplicationCache.ButtonWindowImage != null)

            {
                ApplicationCache.ButtonWindowImage.Dispose();
                ApplicationCache.ButtonWindowImage = null;
            
            }

            if(ApplicationCache.PdfWindowImage != null)
            {
                ApplicationCache.PdfWindowImage.Dispose();
                ApplicationCache.PdfWindowImage = null;
            
            }

            //  read the xml file 
            XDocument docu = XDocument.Load(Path.Combine(Application.StartupPath, GeneralOpt.AppSettingsFileName));
            // load the specific image from the background
            XElement element = docu.Root.Element("BgImages");
            if (element == null)
                return;
            element = docu.Root.Element("BgImages").Element("MainWindow");
            if (element != null)
            {
                string filename = element.Value;
                // procesing the images
                if (File.Exists(Path.Combine(Application.StartupPath, GeneralOpt.BackGroundImageFolder, filename)))
                {

                    // load the images applicaton 
                   
                     Image img = Image.FromFile(Path.Combine(Application.StartupPath, GeneralOpt.BackGroundImageFolder, filename));
                   
                    ApplicationCache.MainFormBGImage = GeneralOpt.ImageResize(img, WinApi.ScreenX, WinApi.ScreenY);
                    ///
                    img.Dispose();

                }

            }
            //
            // load the image for the ButtonWindow
            element = docu.Root.Element("BgImages").Element("ButtonWindow");
            if (element != null)
            {
                string filename = element.Value;
                // procesing the images
                if (File.Exists(Path.Combine(Application.StartupPath, GeneralOpt.BackGroundImageFolder, filename)))
                {
                    // load the images applicaton 
                    Image img = Image.FromFile(Path.Combine(Application.StartupPath, GeneralOpt.BackGroundImageFolder, filename));
                    ApplicationCache.ButtonWindowImage = GeneralOpt.ImageResize(img, WinApi.ScreenX, WinApi.ScreenY);
                    img.Dispose();

                }

            }



            // load the image for the pdfwindow

            element = docu.Root.Element("BgImages").Element("PdfWindow");
            if (element != null)
            {
                string filename = element.Value;
                // procesing the images
                if (File.Exists(Path.Combine(Application.StartupPath, GeneralOpt.BackGroundImageFolder, filename)))
                {
                    // load the images applicaton 
                    Image img = Image.FromFile(Path.Combine(Application.StartupPath, GeneralOpt.BackGroundImageFolder, filename));
                    ApplicationCache.PdfWindowImage = GeneralOpt.ImageResize(img, WinApi.ScreenX, WinApi.ScreenY);
                    img.Dispose();
                }

            }




        }


        // load the goblanessages from the server
        public static void loadTickerMessages()
        {

            DataBaseHandler handler = new DataBaseHandler();
            ApplicationCache.TickerMessages = handler.getGMSG();





        }


        // index image refresh



        public static void LoadBgImages(int pictID)
        {

            //  read the xml file 
            XDocument docu = XDocument.Load(Path.Combine(Application.StartupPath, GeneralOpt.AppSettingsFileName));
            // load the specific image from the background
            XElement element = docu.Root.Element("BgImages");
            if (element == null)
                return;


            switch (pictID)
            {


                case 1:

                    element = docu.Root.Element("BgImages").Element("MainWindow");
                    if (element != null)
                    {
                        string filename = element.Value;
                        // procesing the images
                        if (File.Exists(Path.Combine(Application.StartupPath, GeneralOpt.BackGroundImageFolder, filename)))
                        {
                            // load the images applicaton 
                            Image img = Image.FromFile(Path.Combine(Application.StartupPath, GeneralOpt.BackGroundImageFolder, filename));
                            ApplicationCache.MainFormBGImage = GeneralOpt.ImageResize(img, WinApi.ScreenX, WinApi.ScreenY);
                            ///
                            img.Dispose();

                        }

                    }


                    break;
                //
                // load the image for the ButtonWindow


                case 2:

                    element = docu.Root.Element("BgImages").Element("ButtonWindow");
                    if (element != null)
                    {
                        string filename = element.Value;
                        // procesing the images
                        if (File.Exists(Path.Combine(Application.StartupPath, GeneralOpt.BackGroundImageFolder, filename)))
                        {
                            // load the images applicaton 
                            Image img = Image.FromFile(Path.Combine(Application.StartupPath, GeneralOpt.BackGroundImageFolder, filename));
                            ApplicationCache.ButtonWindowImage = GeneralOpt.ImageResize(img, WinApi.ScreenX, WinApi.ScreenY);
                            img.Dispose();

                        }

                    }


                    break;

                // load the image for the pdfwindow


                case 3:

                    element = docu.Root.Element("BgImages").Element("PdfWindow");
                    if (element != null)
                    {
                        string filename = element.Value;
                        // procesing the images
                        if (File.Exists(Path.Combine(Application.StartupPath, GeneralOpt.BackGroundImageFolder, filename)))
                        {
                            // load the images applicaton 
                            Image img = Image.FromFile(Path.Combine(Application.StartupPath, GeneralOpt.BackGroundImageFolder, filename));
                            ApplicationCache.PdfWindowImage = GeneralOpt.ImageResize(img, WinApi.ScreenX, WinApi.ScreenY);
                            img.Dispose();
                        }

                    }
                    break;




            }




        }


        
        
        // the function that loads the margins settings from the xml file
        public static void loadMarginsSettings()
        { 
        
        
            // settings

            String settingFpath = System.IO.Path.Combine(Application.StartupPath, GeneralOpt.AppSettingsFileName);
            if (File.Exists(settingFpath))
            {

                // XElement docu = XElement.Load(settingFpath);
                XDocument docu = XDocument.Load(settingFpath);
                if (!docu.Root.IsEmpty)
                {


                    XElement margin = docu.Root.Element("Margins");
                    if (margin != null)
                    {

                      
                            XElement temp = margin.Element("MainWindow");
                            if (temp != null && temp.IsEmpty)
                            {
                                 
                                ApplicationCache.mButtomMargin = Convert.ToInt32( temp.Attribute("Buttom").Value);
                                ApplicationCache.mLeftMargin = Convert.ToInt32( temp.Attribute("Left").Value);
                                ApplicationCache.mTopMargin = Convert.ToInt32 (temp.Attribute("Top").Value);
                                ApplicationCache.mRightMargin = Convert.ToInt32(temp.Attribute("Right").Value);
                                ApplicationCache.mWidth = Convert.ToInt32(temp.Attribute("Width").Value);
                                ApplicationCache.mHeight =Convert.ToInt32( temp.Attribute("Height").Value);



                            }
                        

                      
                            // For the button window

                             temp = margin.Element("ButtonWindow");
                            if (temp != null && !temp.IsEmpty)
                            {
                                ApplicationCache.bButtomMargin =Convert.ToInt32( temp.Attribute("Buttom").Value);
                                ApplicationCache.bLeftMargin = Convert.ToInt32(temp.Attribute("Left").Value);
                                ApplicationCache.bTopMargin  = Convert.ToInt32(temp.Attribute("Top").Value);
                                ApplicationCache.bRightMargin = Convert.ToInt32(temp.Attribute("Right").Value);
                                ApplicationCache.bWidth = Convert.ToInt32(temp.Attribute("Width").Value);
                                ApplicationCache.bHeight = Convert.ToInt32(temp.Attribute("Height").Value);
                                ApplicationCache.bNoButton = Convert.ToInt32 (temp.Value);
                                //this.nLabelTxt.Enabled = true;




                            }



                        


                       // For the groupBackWindow window
                         
                             temp = margin.Element("GroupBackButton");
                            if (temp != null && temp.IsEmpty)
                            {
                                ApplicationCache.gButtomMargin = Convert.ToInt32(temp.Attribute("Buttom").Value);
                                ApplicationCache.gLeftMargin = Convert.ToInt32(temp.Attribute("Left").Value);
                                ApplicationCache.gTopMargin = Convert.ToInt32(temp.Attribute("Top").Value);
                                ApplicationCache.gRightMargin = Convert.ToInt32(temp.Attribute("Right").Value);
                                ApplicationCache.gWidth = Convert.ToInt32(temp.Attribute("Width").Value);
                                ApplicationCache.gHeight = Convert.ToInt32(temp.Attribute("Height").Value);


                        }




                    }







                }




            }


        
        
        
        
        
        
        
        }







    } // End of the Application cache class
        

}  // end of the wizard data







